package com.thoughtworks;


import java.util.ArrayList;
import java.util.HashMap;

public class Library {
    private ArrayList<Movie> movie;
    private ArrayList<Book> book;

    public String checkRespectiveUser =null;
    private ArrayList<Book> bookCheckOutArray;
    private ArrayList<Movie> movieCheckOutArray;
    public HashMap<Rental, String> userItemCheckoutMapping;
    public Output output=new Output();


    public Library(ArrayList<Book> book, ArrayList<Movie> movie){
        bookCheckOutArray = new ArrayList<Book>();
        movieCheckOutArray = new ArrayList<Movie>();
        userItemCheckoutMapping = new HashMap<Rental, String>();
        this.book = book;
        this.movie=movie;
    }

    public Library() {

    }

    public boolean checkOutBook(String libraryNumber, String name) {
        Book deleteBook=null;
        boolean flag = false;
        for (Book nameOfBook : book) {
            if (nameOfBook.bookName.equals(name)) {
                flag = true;
                userItemCheckoutMapping.put(nameOfBook, libraryNumber);
                bookCheckOutArray.add(nameOfBook);
                deleteBook = nameOfBook;
            }}
            book.remove(deleteBook);

            return flag;
    }

    public boolean returnBook(String libraryNumber, String name) {
        Book addBook=null;
        boolean flag = false;
        for(Book nameOfBook : bookCheckOutArray)
        if(nameOfBook.bookName.equals(name))
        {
            checkRespectiveUser = userItemCheckoutMapping.get(nameOfBook);
            if(checkRespectiveUser.equals(libraryNumber))
        {
            addBook=nameOfBook;
            book.add(addBook);
            flag=true;
        }}
            bookCheckOutArray.remove(addBook);
            userItemCheckoutMapping.remove(addBook);
            return flag;
    }

    public boolean checkOutMovie(String libraryNumber, String name) {

        Movie deleteMovie=null;
        boolean flag = false;
        for (Movie nameOfMovie : movie) {
            if (nameOfMovie.movieName.equals(name)) {
                flag = true;
                userItemCheckoutMapping.put(nameOfMovie, libraryNumber);
                movieCheckOutArray.add(nameOfMovie);
                deleteMovie = nameOfMovie;

            }}
        movie.remove(deleteMovie);

        return flag;

    }

    public boolean returnMovie(String libraryNumber, String name) {
        Movie addMovie=null;
        boolean flag = false;
        for(Movie nameOfMovie : movieCheckOutArray)
            if(nameOfMovie.movieName.equals(name))
            {
                checkRespectiveUser = userItemCheckoutMapping.get(nameOfMovie);
                if(checkRespectiveUser.equals(libraryNumber))
            {
                addMovie=nameOfMovie;
                movie.add(addMovie);
                flag=true;
            }}
        movieCheckOutArray.remove(addMovie);
        userItemCheckoutMapping.remove(addMovie);
        return flag;
    }

    public String getFormattedBookListing() {
        String result = "";
        for (Book name : book)
            result += String.format("%-50s%-50s%-50d\n", name.bookName, name.authorName, name.yearOfPublication);
        return result;
    }

    public String getFormattedMovieListing() {
        String result = "";
        for (Movie name : movie)
             result+=String.format("%-50s%-50s%-50d%-50d\n", name.movieName, name.directorName, name.yearOfRelease, name.movieRating);
        return result;
    }

    public void getCatalog() {
        String result = "";
        result += String.format("%-50s%-50s%-50s\n\n", "Book Name", "Author Name", "Year of Publication");
        result += getFormattedBookListing();
        result += String.format("\n\n%-50s%-50s%-50s%-50s\n\n", "Movie Name", "Director Name", "Year of Release", "Movie Rating");
        result += getFormattedMovieListing();
        output.displayMessage(result);
    }
}

